package com.hb.software.service;

import com.hb.software.domain.BaseEntity;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class BaseServiceTestCase<PK extends Serializable, T extends BaseEntity<PK>, M extends BaseService<T, PK>>
        extends AbstractJUnit4SpringContextTests {
    protected T entity;
    protected List<T> list;
    protected M manager;

    private Class<T> persistentClass;


    public BaseServiceTestCase(final Class<T> persistentClass) {

        this.persistentClass = persistentClass;
    }

    @Before
    public void setUp() throws Exception {
        this.entity = this.persistentClass.getDeclaredConstructor().newInstance();
    }

    @Test
    public void testCRUD() throws Exception {
        this.manager.deleteAll();

        T t = this.persistentClass.getDeclaredConstructor().newInstance();
        List<T> list = new ArrayList<T>();
        for (Long i = 3000L; i <= 3100L; i++) {
            t.setId(i);
            this.manager.save(t);
            list.add(t);
        }
        T s = this.manager.findOne(3100L);
        assertEquals(t.getId(), s.getId());

        Long count = this.manager.selectCount();
        assertEquals(list.size(), this.manager.findAll().size());

        this.manager.delete(3000L);
        assertEquals(list.size() - 1, this.manager.findAll().size());

        this.manager.deleteAll();
        assertEquals(0, this.manager.findAll().size());


    }

}
